package com.wz.jiangsu.main.problem;

import java.util.HashMap;

/**
 * @program: wz
 * @author: wangzhou
 * @create: 2024-04-28 16:39
 * @description:
 **/
public class TwoSumSecond {
    public static void main(String[] args) {

        int[] arrays = new int[]{2, 7, 5, 12};

        int target = 9;

        int[] getArray = getIntArray(arrays, target);
        StringBuilder sb = new StringBuilder();
        for (int i : getArray) {
            sb.append(i).append(",");
        }

        String substring = sb.substring(0, sb.toString().length() - 1);
        System.out.println(substring);


    }
    public static int[] getIntArray(int[] arrays, Integer target) {

        int[] result = null;
        if (arrays.length < 2) {
            result = new int[]{};
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arrays.length; i++) {
            if (map.containsKey(target - arrays[i])) {
                result = new int[]{map.get(target - arrays[i]),i};
            } else {
                map.put(arrays[i],i);
            }
        }
        return result;
    }
}
